#include "Tools/ScreenMove.h"
//#include "Math/UnrealMathUtility.h"

bool FScreenMoveUnits::ListenScreenMove(APlayerController* PlayerController, const float& MoveSpeed)
{
	return MoveDirection(PlayerController, CursorMove(PlayerController), MoveSpeed);
}

EScreenMoveState FScreenMoveUnits::CursorMove(const APlayerController* PlayerController)
{
	// 屏幕尺寸
	int32 SizeX = INDEX_NONE;
	int32 SizeY = INDEX_NONE;
	// 鼠标位置
	float MousePositionX = -123456.f;
	float MousePositionY = -123456.f;

	if (!PlayerController)
		return EScreenMoveState::Screen_None;
	
	// 获取屏幕尺寸
	PlayerController->GetViewportSize(SizeX, SizeY);
	// 获取鼠标位置
	PlayerController->GetMousePosition(MousePositionX, MousePositionY);
	
	if (FMath::IsNearlyEqual(MousePositionX, 0.0f, 5.0f) && FMath::IsNearlyEqual(MousePositionY, 0.0f, 5.0f))
		return EScreenMoveState::Screen_LeftAndUp;
	else if (FMath::IsNearlyEqual(MousePositionX, 0.0f, 5.0f) && FMath::IsNearlyEqual(SizeY, MousePositionY, 5.0f))
		return EScreenMoveState::Screen_LeftDown;
	else if (FMath::IsNearlyEqual(SizeX, MousePositionX, 5.0f) && FMath::IsNearlyEqual(MousePositionY, 0.0f, 5.0f))
		return EScreenMoveState::Screen_RightAndUp;
	else if (FMath::IsNearlyEqual(SizeX, MousePositionX, 5.0f) && FMath::IsNearlyEqual(SizeY, MousePositionY, 5.0f))
		return EScreenMoveState::Screen_RightAndDown;
	else if (FMath::IsNearlyEqual(MousePositionX, 0.0f, 5.0f))
		return EScreenMoveState::Screen_Left;
	else if (FMath::IsNearlyEqual(SizeX, MousePositionX, 5.0f))
		return EScreenMoveState::Screen_Right;
	else if (FMath::IsNearlyEqual(MousePositionY, 0.0f, 5.0f))
		return EScreenMoveState::Screen_Up;
	else if (FMath::IsNearlyEqual(SizeY, MousePositionY, 5.0f))
		return EScreenMoveState::Screen_Down;
	return EScreenMoveState::Screen_None;
}

bool FScreenMoveUnits::MoveDirection(APlayerController* PlayerController, EScreenMoveState ScreenMoveState, const float& MoveSpeed)
{
	FVector OffsetValue = FVector::ZeroVector;
	if (PlayerController && PlayerController->GetPawn())
	{
		switch (ScreenMoveState)
		{
		case Screen_Up:
			OffsetValue = FVector(1, 0.0f, 0.0f);
			break;
		case Screen_Down:
			OffsetValue = FVector(-1, 0.0f, 0.0f);
			break;
		case Screen_Right:
			OffsetValue = FVector(0.0f, 1, 0.0f);
			break;
		case Screen_Left:
			OffsetValue = FVector(0.0f, -1, 0.0f);
			break;
		case Screen_RightAndUp:
			OffsetValue = FVector(1, 1, 0.0f);
			break;
		case Screen_RightAndDown:
			OffsetValue = FVector(-1, 1, 0.0f);
			break;
		case Screen_LeftAndUp:
			OffsetValue = FVector(1, -1, 0.0f);
			break;
		case Screen_LeftDown:
			OffsetValue = FVector(-1, -1, 0.0f);
			break;
		}
		
		OffsetValue.Normalize();
		PlayerController->GetPawn()->AddActorWorldOffset(OffsetValue * MoveSpeed);
	}

	
	return OffsetValue != FVector::ZeroVector;
}
